package com.victor.core.support;

import java.util.regex.Pattern;

/**
 * Web防火墙工具类
 */
public class WafKit {
    private static Pattern SCRIPT_PATTERN_0 = Pattern.compile("<script>(.*?)</script>", Pattern.CASE_INSENSITIVE);
    private static Pattern SCRIPT_PATTERN_1 = Pattern.compile("</script>", Pattern.CASE_INSENSITIVE);
    private static Pattern SCRIPT_PATTERN_2 = Pattern.compile("<script(.*?)>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
    private static Pattern SCRIPT_PATTERN_3 = Pattern.compile("eval\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
    private static Pattern SCRIPT_PATTERN_4 = Pattern.compile("expression\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
    private static Pattern SCRIPT_PATTERN_5 = Pattern.compile("javascript:", Pattern.CASE_INSENSITIVE);
    private static Pattern SCRIPT_PATTERN_6 = Pattern.compile("vbscript:", Pattern.CASE_INSENSITIVE);
    private static Pattern SCRIPT_PATTERN_7 = Pattern.compile("onload(.*?)=", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);

    /**
     * @param value 待处理内容
     * @return
     * @Description 过滤XSS脚本内容
     */
    public static String stripXSS(String value) {
        String rlt = null;

        if (null != value) {
            // NOTE: It's highly recommended to use the ESAPI library and uncomment the following line to
            // avoid encoded attacks.
            // value = ESAPI.encoder().canonicalize(value);

            // Avoid null characters
            rlt = value.replaceAll("", "");

            // Avoid anything between script tags

            rlt = SCRIPT_PATTERN_0.matcher(rlt).replaceAll("");

            // Avoid anything in a src='...' type of expression
			/*scriptPattern = Pattern.compile("src[\r\n]*=[\r\n]*\\\'(.*?)\\\'", Pattern.CASE_INSENSITIVE
					| Pattern.MULTILINE | Pattern.DOTALL);
			rlt = scriptPattern.matcher(rlt).replaceAll("");

			scriptPattern = Pattern.compile("src[\r\n]*=[\r\n]*\\\"(.*?)\\\"", Pattern.CASE_INSENSITIVE
					| Pattern.MULTILINE | Pattern.DOTALL);
			rlt = scriptPattern.matcher(rlt).replaceAll("");*/

            // Remove any lonesome </script> tag

            rlt = SCRIPT_PATTERN_1.matcher(rlt).replaceAll("");

            // Remove any lonesome <script ...> tag

            rlt = SCRIPT_PATTERN_2.matcher(rlt).replaceAll("");

            // Avoid eval(...) expressions

            rlt = SCRIPT_PATTERN_3.matcher(rlt).replaceAll("");

            // Avoid expression(...) expressions

            rlt = SCRIPT_PATTERN_4.matcher(rlt).replaceAll("");

            // Avoid javascript:... expressions

            rlt = SCRIPT_PATTERN_5.matcher(rlt).replaceAll("");

            // Avoid vbscript:... expressions

            rlt = SCRIPT_PATTERN_6.matcher(rlt).replaceAll("");

            // Avoid onload= expressions

            rlt = SCRIPT_PATTERN_7.matcher(rlt).replaceAll("");
        }

        return rlt;
    }

    /**
     * @param value 待处理内容
     * @return
     * @Description 过滤SQL注入内容
     */
    public static String stripSqlInjection(String value) {
        /**
         * value.replaceAll("('.+--)|(--)|(\\|)|(%7C)", "");
         */
        return (null == value) ? null : value.replaceAll("('.+--)|(--)|(%7C)", "");
    }

    /**
     * @param value 待处理内容
     * @return
     * @Description 过滤SQL/XSS注入内容
     */
    public static String stripSqlXSS(String value) {
        return stripXSS(stripSqlInjection(value));
    }

}
